package net.cruciblesoftware.ghostspacekillah;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.SurfaceHolder;


class KillahThread extends Thread {

    private static final int STATE_INTRO = 1;
    private static final int STATE_RUNNING = 2;
    private static final int STATE_PAUSE = 3;
    private static final int STATE_LOSE = 4;
    private static final int STATE_WIN = 5;

    private boolean running = false;
    private int state = KillahThread.STATE_INTRO;
    private int formerState = state;

    private PlayingField field;
    private SceneBackground background;

    private SurfaceHolder surfaceHolder;
    private Context context;


    public KillahThread(SurfaceHolder sh) {
        surfaceHolder = sh;
    }


    /* Must be called after surface is created */
    public void initField() {
        background = new SceneBackground();
        field = PlayingField.getInstance();
    }


    /* Main loop */
    @Override
    public void run() {
        logi("beginning new thread...");

        long lastTime = System.currentTimeMillis();
        long currentTime;
        float elapsedTime;
        if(running)
            logi("entering main loop...");
        while(running) {
            // time keeping
            currentTime = System.currentTimeMillis();
            elapsedTime = (currentTime - lastTime) / 1000.0f;
            lastTime = currentTime;

            // update state and physics, then attempt to draw
            updateState();
            if(state == KillahThread.STATE_RUNNING) {
                background.update(elapsedTime);
                field.update(elapsedTime);
            }
            Canvas c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized(surfaceHolder) {
                    background.draw(c);
                    field.draw(c);
                }
            } finally {
                if(c != null)
                    surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }


    public void pauseThread(boolean pausing) {
        if(pausing) {
            formerState = state;
            state = KillahThread.STATE_PAUSE;
        }
        else {
            state = formerState;
        }
    }


    public void setRunning(boolean r) {
        running = r;
    }


    private void updateState() {
        switch(state) {
        case KillahThread.STATE_INTRO:
            state = KillahThread.STATE_RUNNING;
            running  = true;
            break;

        case KillahThread.STATE_RUNNING:
            // add check for player state here
            break;

        case KillahThread.STATE_PAUSE:
            break;

        case KillahThread.STATE_WIN:
            logi("state win");
            break;

        case KillahThread.STATE_LOSE:
            logi("state lose");
            break;
        }
    }


    /* Logging functions */
    private void logi(String msg) {
        android.util.Log.i(this.getClass().getName(), msg);
    }
    private void logw(String msg) {
        android.util.Log.w(this.getClass().getName(), msg);
    }
    private void loge(String msg) {
        android.util.Log.e(this.getClass().getName(), msg);
    }
}

